'use strict';

const Controller = require('egg').Controller;

class HouseOrderController extends Controller {
  async houseinformationrequest() {
    const hid = this.ctx.request.query.hid;
    const houseinformationsql = `SELECT price,peonum FROM house WHERE hid=${hid}`;
    const sqlselectres = await this.app.mysql.query(houseinformationsql);
    // console.log(sqlselectres, 1111);
    const obj = { info: sqlselectres };
    this.ctx.body = obj;
  }
  async houseorsersubmit() {
    let ziduan = this.ctx.request.query;
    console.log(ziduan, 777);
    // let uid= this.app.jwt.verify(ziduan.token, 'userid');
    let uid = 8;
    const houseorsersubmitsql = `INSERT INTO house_order (hid,renttime,peoplenum,uid,canrenttime,tenantname,tenantidnumber,tenantphonenumber) VALUES (${ziduan.hid},"${ziduan.renttime}","${ziduan.peoplenum}",${uid},"${ziduan.renttime}","${ziduan.tenantname}","${ziduan.tenantidnumber}",${ziduan.tenantphonenumber})`;
    const sqlselectres = await this.app.mysql.query(houseorsersubmitsql);
    const chaxunoidsql = `SELECT oid FROM house_order WHERE hid=${ziduan.hid} AND tenantphonenumber=${ziduan.tenantphonenumber}`;
    const sqloidres = await this.app.mysql.query(chaxunoidsql);
    this.ctx.body = { info: 'success', data: sqloidres };
  }
  async housecanrenttimerequest() {
    const hid = this.ctx.request.query.hid;
    const housecanrenttimesql = `SELECT canrenttime FROM house_order WHERE hid=${hid}`;
    const sqlselectres = await this.app.mysql.query(housecanrenttimesql);
    // console.log(sqlselectres, 55555);
    const obj = { info: sqlselectres };
    this.ctx.body = obj;
  }
  async orderpay() {
    let oid = this.ctx.request.query.oid;
    // console.log(oid);
    let paysql = `UPDATE house_order SET status=1 WHERE oid=${oid}`;
    let paysqlres = await this.app.mysql.query(paysql);
    this.ctx.body = { code: 2009 };
  }
  async viewingOrderInformation() {
    let hid = this.ctx.request.query.hid;
    console.log(hid)
    let viewhousesql = `SELECT isfree,housetype,place,miaosu FROM house WHERE hid=${hid}`;
    let viewhousesqlres = await this.app.mysql.query(viewhousesql);
    this.ctx.body = { ...viewhousesqlres };
  }
  async userorder() {
    let ziduan = this.ctx.request.query;
    console.log(ziduan.token, 777);
    // let uid= this.app.jwt.verify(ziduan.token, 'userid');
    let hidsql = `SELECT hid FROM house_order WHERE uid=${ziduan.token}`;
    let res = await this.app.mysql.query(hidsql);
    // console.log(res, 777);
    let resultarr = [];
    if (res[0]) {
      for(let j=0;j<res.length;j++){
        let housesql = `SELECT name,pictrue,hid FROM house WHERE hid=${res[j].hid}`;
        let houseres = await this.app.mysql.query(housesql);
        for(let i=0;i<houseres.length;i++){
          resultarr.push(houseres[i]);
        }
      }
      // console.log(resultarr, 666);
      this.ctx.body = { resultarr };
    } 
    
  }
}

module.exports = HouseOrderController;
